<!--# layout("/common/layout.html",{"jsBase":"/js/customer/customerInfo/"}){ -->

<form class="layui-form layui-form-pane" id="theForm" lay-filter="editform" name="theForm">
    <input type="hidden" name="customerId" id="customerId" value="${customerId}">
    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">老师类别</label>
                <div class="layui-input-inline" style="width:512px;">
                    <!--# for(item in roleList) {
                     var check = (item.rightFlag==1?"checked":"");
                    -->
                    <input lay-filter="roleIdChange" type="radio" name="roleId" value="${item.roleId}" title="${item.roleName}" grantprov="${item.grantProv}" ${check}>
                    <!--# } -->
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row layui-hide" id="provDiv">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">授权省份</label>
                <div class="layui-input-inline" style="width:812px;">
                    <input type="checkbox" name="all" id="all" title="全国" value="all" lay-skin="primary" lay-filter="checkAll">
                    <!--# for(prov in provinceList) {
                     var provCheck = (prov.provFlag==1?"checked":"");
                    -->
                    <input type="checkbox" name="provIds" class="checkprovince"  title="${prov.provName}" value="${prov.provId}" lay-skin="primary" ${provCheck}>
                    <!--# } -->

                </div>
            </div>
        </div>
    </div>

    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">有效截止期</label>
                <div class="layui-input-inline">
                    <input type="text" name="expiredDate"  id="expiredDate" autocomplete="off" class="layui-input input-datetime" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">有效天数</label>
                <div class="layui-input-inline">
                    <input type="text" name="validityDay" id="validityDay" autocomplete="off" maxlength="50" class="layui-input"
                           onkeyup="carNum($(this))" onafterpaste="this.value=this.value.replace(/\D/g,'')" />
                </div>
            </div>
        </div>
    </div>

    <div class="layui-row">
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-danger" type="button" id="addButton" lay-submit lay-filter="form">保存</button>
            <button class="layui-btn layui-btn-primary" style="display:none" type="button" id="addButton-loading" >保存中...</button>
            <button class="layui-btn" type="button" id="addButton-cancel">取消</button>
            <!--# if(rightFlag){ -->
            <button class="layui-btn layui-btn-danger" type="button" id="revokeButton" lay-submit lay-filter="form">收回授权</button>
            <!--# } -->
        </div>
    </div>

</form>
<!--#} -->
<script>
    layui.define([ 'form', 'laydate', 'table','customerInfoApi'], function(exports) {
        var form = layui.form;
        var customerInfoApi = layui.customerInfoApi;
        var view = {
            init:function(){
                Lib.initGenrealForm($("#theForm"),form);
                this.initSubmit();
                <!--# for(item in roleList) {
                    if (item.rightFlag==1) {
                -->
                this.changeRole(${item.grantProv});
                <!--#
                    }
                } -->
                var _view = this;
                form.on('radio(roleIdChange)', function(data){
                    _view.changeRole($(data.elem).attr("grantprov"));
                });
                form.on('checkbox(checkAll)',function(data){
                    $(".checkprovince").each(function(idx,elem) {
                        elem.checked = data.elem.checked;
                    });
                    form.render("checkbox")
                });
            },
            changeRole:function(grantProv) {
                if (grantProv>0) {
                    $("#provDiv").removeClass("layui-hide");
                } else {
                    $("#provDiv").removeClass("layui-hide");
                    $("#provDiv").addClass("layui-hide");
                }
            },
            initSubmit:function(){
                $("#addButton").click(function(){
                    form.on('submit(form)', function(){
                        var formVal = form.val("editform");
                        if (!("roleId" in formVal)) {
                            Common.alert("必须选择老师类别",2);
                            return false;
                        }
                        var checkProv = false;
                        $(":radio").each(function () {
                           if(this.checked && $(this).attr("grantprov")==1) {
                               checkProv = true;
                           }
                        });
                        if (checkProv && !("provIds" in formVal)) {
                            Common.alert("该老师类别必须选择授权省份！",2);
                            return false;
                        }
                        customerInfoApi.grantForm($('#theForm'),function(){
                            parent.window.dataReload();
                            Common.info("授权成功。");
                            Lib.closeFrame();
                        });
                    });
                });

                $("#addButton-cancel").click(function(){
                    Lib.closeFrame();
                });

                $("#revokeButton").click(function(){
                    Common.openConfirm("确认收回授权？",function(){
                        customerInfoApi.revoke($('#theForm'),function(){
                            parent.window.dataReload();
                            Common.info("收回授权成功。");
                            Lib.closeFrame();
                        });
                    });
                });
            }

        }
        view.init();
    });

    /********input表单只允许输入大于0的整数，没有小数点************/
    function carNum($this){
        //输入框的值
        var value = $this.val() ;

        if(isNaN(value)){//判断值是不是数字
            $this.val(1) ;
        }else if(value==""){//这是当只有1位的时候，删除这个会进入这个判断，如果没有该判断，当只有一位的时候就不能删除

        }else if(value==0){//判断值是不是1
            $this.val(1) ;
        }else if(value.indexOf(".")!=-1){//判断有没有输入小数点
            $this.val(value.substring(0,value.indexOf(".")))
        }
    }
</script>
